﻿using System;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using PushEmail.Storage;

namespace PushEmail.Common.Logging
{
	/// <summary>
	/// This logging object writes application error and debug output using the
	/// <see cref="System.Diagnostics.Trace"/> facilities.
	/// </summary>
	public class DiagnosticsLogger : ILog
	{
        public const string LOG_DIR = "logdata";
        private static System.Object lockObject = new System.Object();

		/// <summary>
		/// Logs an error message to the System Trace facility
		/// </summary>
		/// <param name="message">This is the error message to log</param>
		public void LogError(string message)
		{
			if(message == null)
				throw new ArgumentNullException("message");

		//	System.Diagnostics.Trace.WriteLine("OpenPOP: " + message);
		}

		/// <summary>
		/// Logs a debug message to the system Trace Facility
		/// </summary>
		/// <param name="message">This is the debug message to log</param>
		public void LogDebug(string message)
		{
            Debug.WriteLine(message);
            writerLog(message);
			if (message == null)
				throw new ArgumentNullException("message");
		}

        private void writerLog(string message)
        {
            lock (lockObject)
            {
                string logFilePath = LOG_DIR + "\\" + "log.log";

                IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

                if (!IsolateStorageUtil.IsExistFile(logFilePath))
                {
                    if (!myIsolatedStorage.DirectoryExists(LOG_DIR))
                    {
                        myIsolatedStorage.CreateDirectory(LOG_DIR);
                    }

                    using (StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(logFilePath, FileMode.CreateNew, myIsolatedStorage)))
                    {
                        writer.WriteLine("\r\n---------------------------错误日志-------------------------------");
                        writer.WriteLine("错误发生时间:" + DateTime.Now);
                        writer.Write(message);
                        writer.Close();
                        writer.Dispose();
                    }

                }
                else
                {
                    DateTimeOffset df =  myIsolatedStorage.GetLastWriteTime(logFilePath);

                    DateTimeOffset dfNow = DateTimeOffset.Now;
                    dfNow = dfNow.AddDays(-2);
                    FileMode fM = FileMode.Append;
                    if (dfNow >= df)
                        fM = FileMode.Truncate;

                    using (StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(logFilePath, fM, myIsolatedStorage)))
                    {
                        writer.WriteLine("\r\n---------------------------Error Log-------------------------------");
                        writer.WriteLine("Error at Time:" + DateTime.Now);
                        writer.Write(message);
                        writer.Close();
                        writer.Dispose();
                    }
                }

            }
            
 
        }
	}
}